package com.tanxiaoyao.wecon.ui;

import com.tanxiaoyao.wecon.R;
import com.tanxiaoyao.wecon.app.AppManager;
import com.tanxiaoyao.wecon.net.BluetoothConnectService;
import com.tanxiaoyao.wecon.net.GlobalConnectHelper;

import android.R.integer;
import android.app.Activity;
import android.os.Bundle;
import android.test.PerformanceTestCase;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

/**
 * Created by acer on 2015/1/10.
 */
public class ActivityMouseMain extends Activity {
	// DEBUG标志
	private static final String TAG = "tanxiaoyao-mouse-main";
	private static final Boolean ISDEBUG = false;
	// 声明控件
	private Button leftButton, rightButton;
	private FrameLayout touchPanel;
	// 声明字符串常量
	private final String LEFT_BTN_DOWN = "left_button_down";
	private final String LEFT_BTN_UP = "left_button_up";
	private final String RIGHT_BTN_DOWN = "right_button_down";
	private final String RIGHT_BTN_UP = "right_button_up"; // 鼠标按键
	private final String TOUCH_DOWN = "touch_down";
	private final String TOUCH_UP = "touch_up";
	private final String TOUCH_MOVE = "touch_move";
	// 坐标记录量
	private static float mx = 0; // 发送的鼠标移动的差值
	private static float my = 0;
	private static float lx; // 记录上次鼠标的位置
	private static float ly;
	private static float fx; // 手指第一次接触屏幕时的坐标
	private static float fy;
	private static float lbx = 0; // 按住鼠标左键移动初始化坐标
	private static float lby = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//      Activity压栈
      AppManager.getAppManager().addActivity(this);
		// 设置布局
		setContentView(R.layout.activity_mouse_main);
		// 实例化控件
		leftButton = (Button) findViewById(R.id.left_button);
		rightButton = (Button) findViewById(R.id.right_button);
		touchPanel = (FrameLayout) findViewById(R.id.touch_panel);
		// 为界面组件绑定监听器
		boundListener();

	}

	private void boundListener() {
		// 注册按钮监听器
		leftButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					GlobalConnectHelper.globalConnector.sendMess(LEFT_BTN_DOWN);
					break;
				case MotionEvent.ACTION_UP:
					lbx = 0;
					lby = 0; // 这个初始化是不是必须的？我先不要试试
					GlobalConnectHelper.globalConnector.sendMess(LEFT_BTN_UP);
					break;
				case MotionEvent.ACTION_MOVE:
					moveTouchSecondPoint(event);
					break;
				default:
					break;
				}
				return false;
			}
		});
		rightButton.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					GlobalConnectHelper.globalConnector
							.sendMess(RIGHT_BTN_DOWN);
					break;
				case MotionEvent.ACTION_UP:
					GlobalConnectHelper.globalConnector.sendMess(RIGHT_BTN_UP);
					break;
				default:
					break;
				}
				return false;
			}

		});
		// 组册触控板监听器
		touchPanel.setOnTouchListener(new View.OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_MOVE)
					onTouchMove(event);
				if (event.getAction() == MotionEvent.ACTION_DOWN)
					onTouchDown(event);
				if (event.getAction() == MotionEvent.ACTION_UP)
					onTouchUp(event);
				return true;
			}

		});

	}

	/**
	 * 触摸移动事件
	 * 
	 * @param ev
	 *            触摸事件
	 */
	private void onTouchMove(MotionEvent ev) { // 鼠标移动事件处理
		float x = ev.getX();
		mx = x - lx; // 当前鼠标位置 - 上次鼠标的位置
		lx = x; // 把当前鼠标的位置付给lx 以备下次使用
		float y = ev.getY();
		my = y - ly;
		ly = y;
		if (mx != 0 && my != 0)
			sendTouchEvent(TOUCH_MOVE, mx, -my); // （转化为数学坐标系）
													// 将时间中当前的坐标取出，与上次存储的坐标作差得出移动量
		// （那第一次和什么做差呢？难道系统本身没有直接得出移动量的事件吗，我还有一个类似的例子，待会再看看那个）

	}

	/**
	 * 触摸板弹起
	 * 
	 * @param event
	 *            触摸事件
	 */
	private void onTouchUp(MotionEvent event) {
		// 如果手指按下和手指离开的坐标相同就是只是一次单纯的按下动作，向电脑发送按下松开指令
		if (fx == event.getX() && fy == event.getY()) {
			GlobalConnectHelper.globalConnector.sendMess(TOUCH_DOWN);
			GlobalConnectHelper.globalConnector.sendMess(TOUCH_UP);
		}
	}

	/**
	 * 触摸板按下
	 * 
	 * @param event
	 *            触摸事件
	 */
	private void onTouchDown(MotionEvent event) {
		lx = event.getX(); // 当手指第一放入时 把当前坐标付给lx
		ly = event.getY();
		fx = event.getX();
		fy = event.getY(); // 手指刚放上去这里对初始量做了初始化操作

	}

	/**
	 * 触摸板按住左键操作
	 * 
	 * @param event
	 *            另一个手指的触摸动作事件
	 */
	private void moveTouchSecondPoint(MotionEvent event) {
		int count = event.getPointerCount();
		if (count == 2) { // 只适配了双指操作
			if (lbx == 0 && lby == 0) { // 这种判断初始状态的方法其实不好，虽然实际用手指很难移动到（0，0）处
				lbx = event.getX(1); // 编号为1 的手指？？？
				lby = event.getY(1);
				return;
			}
			float x = event.getX(1);
			float y = event.getY(1);
			sendTouchEvent(TOUCH_MOVE, x - lbx, -(y - lby)); // 也是作差取得移动量(这个实际上也是发送的MOUSE_MOVE)
			lbx = x;
			lby = y;
		}
		if (count == 1) {
			lbx = 0;
			lby = 0; // 单点触摸之后清除记录量
		}
	}

	/**
	 * 发送触摸板事件
	 * 
	 * @param type
	 *            动作类型
	 * @param x
	 *            横坐标参数
	 * @param y
	 *            纵坐标参数
	 */
	private void sendTouchEvent(String type, float x, float y) {
//		强转为int
		int x_pos = (int)x;
		int y_pos = (int)y;
		
		// 这里的移动事件我们只发送移动量就可以了，具体坐标其实取决于电脑屏幕上箭头的位置
		switch (type) {
		case TOUCH_DOWN:
			GlobalConnectHelper.globalConnector.sendMess(TOUCH_DOWN + ":" + x_pos
					+ "," + y_pos);
			break;
		case TOUCH_UP:
			GlobalConnectHelper.globalConnector.sendMess(TOUCH_UP + ":" + x_pos
					+ "," + y_pos);
			break;
		case TOUCH_MOVE:
			GlobalConnectHelper.globalConnector.sendMess(TOUCH_MOVE + ":" + x_pos
					+ "," + y_pos);
			break;
		default:
			break;
		}
	}
}